package cn.com.open.itembank.controller;

import cn.com.open.itembank.dto.*;
import cn.com.open.itembank.entity.Question;
import cn.com.open.itembank.exception.PoiException;
import cn.com.open.itembank.service.ImportService;
import cn.com.open.itembank.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by lucky on 2017/9/19.
 */
@RestController
@RequestMapping(value = "/question")
public class QuestionController {
    @Autowired
    private ItemService service;
    @Autowired
    private ImportService importService;

    /**
     * 获取指定试题基本信息.
     *
     * @param id 试题id
     * @return 试题主体模型 full item
     */
    @GetMapping(value = "/{id}")
    public Question getItem(@PathVariable(value = "id") String id) {
        return service.getItem(id);
    }

    /**
     * 根据试题ID获取试题的试题应用类别信息
     *
     * @param id 试题ID
     * @return 试题应用类别信息 item question category
     */
    @GetMapping(value = "/{id}/category")
    public ReturnModel getItemQuestionCategory(@PathVariable(value = "id") String id) {
        return service.getQuestionCategory(id);
    }

    /**
     * 根据试题数量查询模型获取指定条件下试题数量（策略用）.
     *
     * @param filterModels the filter model
     * @return the question count for rules check
     */
    @PostMapping(value = "/rulecheck/count")
    public Map<String, Integer> getQuestionCountForRulesCheck(@RequestBody Map<String, QuestionCountFilterModel> filterModels) {
        return service.getQuestionCountForRulesCheck(filterModels);
    }

    /**
     * 根据试题数量查询模型获取指定条件下试题（策略组卷用）.
     *
     * @param filterModels the filter model
     * @return the question count for rules check
     */
    @PostMapping(value = "/rulecheck/questions")
    public Map<String, List<RuleQuestionModel>> getQuestionForRule(@RequestBody Map<String, QuestionCountFilterModel> filterModels) {
        return service.getQuestionForRule(filterModels);
    }

    /**
     * 根据题库id，题型id，所属类别id查询试题id列表（策略组卷用）.
     *
     * @param itemBankId     the item bank id
     * @param typeCategories the type categories
     * @return the questions by categories for rules
     */
    @PostMapping(value = "/rulecheck/categories/questions")
    public Map<String, List<String>> getQuestionsByCategoriesForRules(@RequestParam(value = "itemBankId") String itemBankId, @RequestBody Map<String, String> typeCategories) {
        return service.getQuestionsByCategoriesForRules(itemBankId, typeCategories);
    }

    /**
     * 根据试题id列表获取策略检查用的试题信息列表（策略检查必选和例题用）.
     *
     * @param itemIds the item ids
     * @return the question info for rule check
     */
    @PostMapping(value = "/rulecheck/info")
    public List<RuleCheckQuestionInfo> getQuestionInfoForRuleCheck(@RequestBody Map<String, Boolean> itemIds) {
        return service.getQuestionInfoForRuleCheck(itemIds);
    }

    /**
     * 导入逻辑.
     *
     * @param importParameter the input model
     * @return the return model
     * @throws PoiException the poi exception
     */
    @PostMapping(value = "/import")
    public ReturnModelForTestPaper importLogic(@RequestBody ImportParameter importParameter) throws PoiException {
        return importService.importLogic(importParameter);
    }
}
